<template>
  <div class="markdown-content">
    <!-- 流式输出时显示原始文本 + 光标 -->
    <div v-if="isStreaming" class="streaming-text">
      {{ content }}<span class="typing-cursor">|</span>
    </div>
    <!-- 非流式输出时渲染 markdown -->
    <div v-else v-html="renderedMarkdown"></div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { marked } from 'marked'
import hljs from 'highlight.js'
import 'highlight.js/styles/github.css'

const props = defineProps({
  content: {
    type: String,
    required: true,
  },
  isStreaming: {
    type: Boolean,
    default: false,
  },
})

// Configure marked options
marked.setOptions({
  highlight: function (code, lang) {
    const language = hljs.getLanguage(lang) ? lang : 'plaintext'
    return hljs.highlight(code, { language }).value
  },
  breaks: true,
  gfm: true,
  pedantic: false,
  sanitize: false,
  smartLists: true,
  smartypants: false,
})

const renderedMarkdown = computed(() => {
  if (!props.content) return ''
  
  try {
    let html = marked(props.content)
    // 清理多余空白
    html = html.replace(/>\s+</g, '><')
    html = html.replace(/^\s+|\s+$/g, '')
    return html
  } catch (error) {
    console.error('Markdown rendering error:', error)
    return props.content
  }
})
</script>

<style scoped>
.markdown-content {
  line-height: 1.6;
  width: 100%;
  max-width: 100%;
  word-wrap: break-word;
  overflow-wrap: break-word;
  font-family: 'Courier New', monospace;
  color: #ffffff;
  text-shadow: 0 0 3px rgba(0, 255, 255, 0.3);
}

.streaming-text {
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  font-family: 'Courier New', monospace;
  color: #ffffff;
  text-shadow: 0 0 3px rgba(0, 255, 255, 0.3);
  line-height: 1.6;
  min-height: 1.2em; /* 确保即使内容为空也有高度 */
}

.typing-cursor {
  display: inline-block;
  width: 2px;
  height: 1.2em;
  background-color: #00ffff;
  animation: blink 1s infinite;
  margin-left: 2px;
  vertical-align: text-bottom;
}

@keyframes blink {
  0%,
  50% {
    opacity: 1;
  }
  51%,
  100% {
    opacity: 0;
  }
}

.markdown-content :deep(h1) {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0.5rem 0 0.25rem 0;
  color: #00ffff;
  text-shadow:
    0 0 10px #00ffff,
    0 0 20px #00ffff,
    0 0 30px #00ffff;
  animation: neonGlow 2s ease-in-out infinite;
  text-transform: uppercase;
  letter-spacing: 2px;
  border-bottom: 2px solid #00ffff;
  padding-bottom: 0.25rem;
}

.markdown-content :deep(h2) {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0.4rem 0 0.2rem 0;
  color: #ff00ff;
  text-shadow:
    0 0 10px #ff00ff,
    0 0 20px #ff00ff,
    0 0 30px #ff00ff;
  animation: neonGlow 2s ease-in-out infinite;
  text-transform: uppercase;
  letter-spacing: 1px;
  border-bottom: 1px solid #ff00ff;
  padding-bottom: 0.15rem;
}

.markdown-content :deep(h3) {
  font-size: 1.125rem;
  font-weight: 600;
  margin: 0.3rem 0 0.15rem 0;
  color: #00ffff;
  text-shadow: 0 0 8px rgba(0, 255, 255, 0.8);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.markdown-content :deep(p) {
  margin: 0.2rem 0;
  color: #ffffff;
  text-shadow: 0 0 3px rgba(0, 255, 255, 0.3);
}

.markdown-content :deep(ul) {
  margin: 0.2rem 0;
  padding-left: 1.5rem;
  color: #ffffff;
}

.markdown-content :deep(ol) {
  margin: 0.2rem 0;
  padding-left: 1.5rem;
  color: #ffffff;
}

.markdown-content :deep(li) {
  margin: 0.1rem 0;
  position: relative;
}

.markdown-content :deep(li::before) {
  content: '▶';
  color: #00ffff;
  font-size: 0.75rem;
  margin-right: 0.5rem;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.8);
}

.markdown-content :deep(blockquote) {
  border-left: 3px solid #00ffff;
  margin: 0.3rem 0;
  padding-left: 1rem;
  color: rgba(0, 255, 255, 0.8);
  font-style: italic;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 12px;
  backdrop-filter: blur(5px);
}

.markdown-content :deep(code) {
  background: rgba(0, 0, 0, 0.6);
  color: #00ffff;
  padding: 0.125rem 0.25rem;
  border-radius: 8px;
  font-size: 0.875em;
  font-family: 'Courier New', monospace;
  border: 1px solid rgba(0, 255, 255, 0.3);
  text-shadow: 0 0 3px rgba(0, 255, 255, 0.5);
  box-shadow: 0 0 10px rgba(0, 255, 255, 0.2);
}

.markdown-content :deep(pre) {
  background: rgba(0, 0, 0, 0.8);
  border: 1px solid #00ffff;
  border-radius: 15px;
  padding: 0.75rem;
  margin: 0.3rem 0;
  overflow-x: auto;
  max-width: 100%;
  word-wrap: break-word;
  white-space: pre-wrap;
  backdrop-filter: blur(5px);
  box-shadow:
    0 4px 15px rgba(0, 255, 255, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.markdown-content :deep(pre code) {
  background: none;
  padding: 0;
  color: inherit;
  border: none;
  box-shadow: none;
}

.markdown-content :deep(table) {
  border-collapse: collapse;
  margin: 0.3rem 0;
  width: 100%;
  max-width: 100%;
  table-layout: fixed;
  border: 1px solid #00ffff;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.3);
}

.markdown-content :deep(th),
.markdown-content :deep(td) {
  border: 1px solid rgba(0, 255, 255, 0.3);
  padding: 0.5rem;
  text-align: left;
  background: rgba(0, 0, 0, 0.4);
}

.markdown-content :deep(th) {
  background: linear-gradient(135deg, rgba(0, 255, 255, 0.2) 0%, rgba(255, 0, 255, 0.2) 100%);
  font-weight: 600;
  color: #00ffff;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.8);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.markdown-content :deep(tr:hover) {
  background: rgba(0, 255, 255, 0.1);
}

.markdown-content :deep(a) {
  color: #00ffff;
  text-decoration: none;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.8);
  transition: all 0.3s ease;
  position: relative;
}

.markdown-content :deep(a::before) {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, #00ffff, #ff00ff);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.markdown-content :deep(a:hover) {
  color: #ffffff;
  text-shadow: 0 0 10px rgba(0, 255, 255, 1);
}

.markdown-content :deep(a:hover::before) {
  transform: scaleX(1);
}

.markdown-content :deep(strong) {
  font-weight: 700;
  color: #00ffff;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.8);
}

.markdown-content :deep(em) {
  font-style: italic;
  color: #ff00ff;
  text-shadow: 0 0 5px rgba(255, 0, 255, 0.8);
}

.markdown-content :deep(hr) {
  border: none;
  border-top: 2px solid #00ffff;
  margin: 0.5rem 0;
  background: linear-gradient(90deg, transparent, #00ffff, transparent);
  height: 2px;
  border-radius: 1px;
  box-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}

/* Additional cyberpunk effects */
.markdown-content :deep(.highlight) {
  background: linear-gradient(135deg, rgba(0, 255, 255, 0.2) 0%, rgba(255, 0, 255, 0.2) 100%);
  padding: 0.125rem 0.25rem;
  border-radius: 8px;
  border: 1px solid #00ffff;
  color: #ffffff;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.8);
}

.markdown-content :deep(.warning) {
  background: rgba(255, 0, 0, 0.2);
  border: 1px solid #ff0000;
  color: #ff6b6b;
  padding: 0.5rem;
  border-radius: 12px;
  margin: 0.25rem 0;
}

.markdown-content :deep(.success) {
  background: rgba(0, 255, 0, 0.2);
  border: 1px solid #00ff00;
  color: #4ade80;
  padding: 0.5rem;
  border-radius: 12px;
  margin: 0.25rem 0;
}

.markdown-content :deep(.info) {
  background: rgba(0, 100, 255, 0.2);
  border: 1px solid #0064ff;
  color: #60a5fa;
  padding: 0.5rem;
  border-radius: 12px;
  margin: 0.25rem 0;
}

/* Animated elements */
.markdown-content :deep(.cyber-button) {
  background: linear-gradient(135deg, #00ffff 0%, #ff00ff 100%);
  border: 1px solid #00ffff;
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 12px;
  text-decoration: none;
  display: inline-block;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(0, 255, 255, 0.4);
}

.markdown-content :deep(.cyber-button:hover) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 255, 255, 0.6);
}

@keyframes neonGlow {
  0%,
  100% {
    text-shadow:
      0 0 5px currentColor,
      0 0 10px currentColor,
      0 0 15px currentColor;
  }
  50% {
    text-shadow:
      0 0 10px currentColor,
      0 0 20px currentColor,
      0 0 30px currentColor;
  }
}

/* Responsive markdown */
@media (max-width: 768px) {
  .markdown-content :deep(h1) {
    font-size: 1.25rem;
    letter-spacing: 1px;
  }

  .markdown-content :deep(h2) {
    font-size: 1.125rem;
    letter-spacing: 0.5px;
  }

  .markdown-content :deep(h3) {
    font-size: 1rem;
  }

  .markdown-content :deep(table) {
    font-size: 0.875rem;
  }

  .markdown-content :deep(th),
  .markdown-content :deep(td) {
    padding: 0.25rem;
  }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
  .markdown-content :deep(*),
  .markdown-content :deep(a::before) {
    transition: none;
    animation: none;
  }
}

/* Syntax highlighting specific styles */
.markdown-content :deep(.hljs) {
  background: #f8fafc;
  color: #1f2937;
}

.markdown-content :deep(.hljs-keyword),
.markdown-content :deep(.hljs-selector-tag),
.markdown-content :deep(.hljs-addition) {
  color: #059669;
}

.markdown-content :deep(.hljs-number),
.markdown-content :deep(.hljs-string),
.markdown-content :deep(.hljs-meta .hljs-string),
.markdown-content :deep(.hljs-literal),
.markdown-content :deep(.hljs-doctag),
.markdown-content :deep(.hljs-regexp) {
  color: #dc2626;
}

.markdown-content :deep(.hljs-title),
.markdown-content :deep(.hljs-section),
.markdown-content :deep(.hljs-name),
.markdown-content :deep(.hljs-selector-id),
.markdown-content :deep(.hljs-selector-class) {
  color: #3b82f6;
}

.markdown-content :deep(.hljs-attribute),
.markdown-content :deep(.hljs-attr),
.markdown-content :deep(.hljs-variable),
.markdown-content :deep(.hljs-template-variable),
.markdown-content :deep(.hljs-class .hljs-title),
.markdown-content :deep(.hljs-type) {
  color: #ea580c;
}

.markdown-content :deep(.hljs-symbol),
.markdown-content :deep(.hljs-bullet),
.markdown-content :deep(.hljs-subst),
.markdown-content :deep(.hljs-meta),
.markdown-content :deep(.hljs-meta .hljs-keyword),
.markdown-content :deep(.hljs-selector-attr),
.markdown-content :deep(.hljs-selector-pseudo),
.markdown-content :deep(.hljs-link) {
  color: #7c3aed;
}

.markdown-content :deep(.hljs-built_in),
.markdown-content :deep(.hljs-deletion) {
  color: #dc2626;
}

.markdown-content :deep(.hljs-formula) {
  background: #f3f4f6;
}

.markdown-content :deep(.hljs-emphasis) {
  font-style: italic;
}

.markdown-content :deep(.hljs-strong) {
  font-weight: bold;
}
</style>
